package com.baymax.test.scene.demo

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import com.baymax.test.scene.R
import com.bytedance.scene.Scene
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 * @title:       标题
 * @project:     cfzx.android
 * @package:     com.cfzx.debug.demos
 * @class:       ConstrainRemoveTestScene
 * @description:
 * <p>
 * 类描述
 * </p>
 *
 * @version:   1.0
 * @createDate:   2022/1/20 18:54
 * @Copyright (C) 2022 YSTEN
 * @author:       xuyujian
 */
class ConstrainRemoveTestScene() : Scene(), CoroutineScope by MainScope() {


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup,
        savedInstanceState: Bundle?
    ): View = inflater.inflate(R.layout.layer, container, false)

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val scene = this
        val cs = ConstraintSet()
        val v1 = view.findViewById<View>(R.id.v1)
        val v3 = view.findViewById<View>(R.id.v3)
        val v4 = view.findViewById<View>(R.id.v4)
        val bg = view.findViewById<View>(R.id.bg)
        val ba = view.findViewById<View>(R.id.ba)
        cs.clone(view as ConstraintLayout)

        launch {

            delay(1000)
            cs.constrainWidth(v3.id, 180)
            cs.applyTo(view)
            delay(2000)
            cs.connect(bg.id, ConstraintSet.END,  ba.id, ConstraintSet.END ,0)
            cs.applyTo(view)
            delay(2000)
            cs.constrainWidth(v3.id ,ConstraintSet.MATCH_CONSTRAINT)
            cs.connect(bg.id, ConstraintSet.END, v4.id, ConstraintSet.START , 15 )
            cs.applyTo(view)
        }

//        (view as ConstraintLayout).apply {
//            val v = View(requireSceneContext())
//            addView(v.apply {
//                background = DrawableUtils.createGradientDrawable(
//                    intArrayOf(
//                        Color.argb(255, 0, 0, 0),
//                        Color.argb(183, 0, 0, 0),
//                        Color.argb(0, 0, 0, 0),
//                    ), Color.TRANSPARENT, 0, 0f
//                )
//
//            }, 0, ConstraintLayout.LayoutParams(0, 0).apply {
//                startToStart = ConstraintLayout.LayoutParams.PARENT_ID
//                endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
//                topToTop = ConstraintLayout.LayoutParams.PARENT_ID
//                endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
//                matchConstraintPercentHeight = 0.5f
//                matchConstraintPercentWidth = 0.5f
//                horizontalBias = 1f
//                verticalBias = 0f
//            })
//
//
//            val animator1 = {
//                val key1 = Keyframe.ofFloat(0f, 0f)
//                val key2 = Keyframe.ofFloat(0.2f, 600f)
//                val key3 = Keyframe.ofFloat(0.5f, 50f)
//                val key4 = Keyframe.ofFloat(1f, 300f)
//                val holder = PropertyValuesHolder.ofKeyframe("translationX", key1, key2, key3, key4)
//                val animator = ObjectAnimator.ofPropertyValuesHolder(v, holder)
//                animator.addListener(
//                    onStart = { println("onStart") },
//                    onEnd = { println("onEnd") },
//                )
//                animator.duration = 1000
//                animator.start()
//            }
////            animator1()
//
//
//
//            val animator2 = {
//               val alpha  =  PropertyValuesHolder.ofFloat("alpha" , 1f ,0.6f, 0.2f ,0f)
//               val translationX  =  PropertyValuesHolder.ofFloat("translationX" , 0f,0f,0f ,0f, 10f ,20f ,40f ,60f, 100f)
//
//                val animator = ObjectAnimator.ofPropertyValuesHolder(v ,alpha ,translationX)
//                animator.addListener(
//                    onStart = { println("onStart") },
//                    onEnd = { println("onEnd") },
//                )
//                animator.addUpdateListener {
//                    println("${it.animatedValue}")
//                }
//                animator.duration = 3000
//                animator.start()
//            }
//            animator2()
//        }
    }


//    fun test(p: ConstraintLayout) {
//        launch {
//            val tv = TextView(requireActivity()).apply {
//                id = R.id.textSpacerNoButtons
//                textSize = 16f
//                text = "this will remove"
//                val lp = ConstraintLayout.LayoutParams(200, 200)
//                    .apply {
//                        topToTop = ConstraintLayout.LayoutParams.PARENT_ID
//                        bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
//                        startToStart = ConstraintLayout.LayoutParams.PARENT_ID
//                        endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
//                    }
//                p.addView(this, lp)
//            }
//
//            delay(1200)
//            p.removeView(tv)
//            delay(1200)
//            p.setBackgroundColor(Color.GREEN)
//            p.invalidate()
//            p.requestLayout()
//        }
//
//    }

    class AnimatorValue<T>(val prop: String, var value: T)
}